package class202205.demo15.demo13.Queue;

public class MyLinkedList {
    //定义节点
    class Node{
        public int val;
        public Node next;

        //提供一个构造方法
        public Node(int val){
            this.val = val;
        }

    }

    public Node head;
    public Node last;
    public int usedSize;

    /**
     * 入队
     * @param val
     */
    public void offer(int val){
        //new一个node
        Node node = new Node(val);
        //第一次插入
        if (head == null){
            head = node;
            last = node;
        }else {
            last.next = node;
            last = node;
        }
        this.usedSize++;
    }

    /**
     * 出队
     * @return
     */
    public int poll(){
        if (isEmpty()){
            throw new RuntimeException("队列为空！");
        }
        int val = head.val;
        head = head.next;
        //最后一个节点的处理
        if (head == null){
            last = null;
        }
        usedSize--;
        return val;
    }

    /**
     * 获取队头元素 但不删除
     * @return
     */
    public int peek(){
        if (isEmpty()){
            throw new RuntimeException("队列为空！");
        }
        return head.val;
    }

    public int size(){
        return usedSize;
    }

    public boolean isEmpty(){
        return usedSize == 0;
    }

}
